package com.leo.foodmaster.admin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.commons.annotation.Log;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.enums.BusinessType;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import com.leo.foodmaster.admin.system.entity.SysNoticeEntity;
import com.leo.foodmaster.admin.system.service.ISysNoticeService;
import com.leo.foodmaster.admin.system.mapper.SysNoticeMapper;
import javax.annotation.Resource;

/**
 * 通知公告表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-12-22
 */
@RestController
@RequestMapping("/system/notice")
@Api(tags = "通知公告表")
public class SysNoticeController {
    @Resource
    private ISysNoticeService isysNoticeService;
    @Resource
    private SysNoticeMapper sysNoticeMapper ;


    @GetMapping("/pageList")
    @ApiOperation("条件分页查询")
    public Result<IPage<SysNoticeEntity>> page(@ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
                                               @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
                                               @ApiParam(value = "公告标题") @RequestParam(required = false) String noticeTitle,
                                               @ApiParam(value = "操作人员") @RequestParam(required = false) String createBy,
                                               @ApiParam(value = "通知类型") @RequestParam(required = false) String noticeType,
                                               @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
                                               @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime){
    	IPage<SysNoticeEntity> pageList = isysNoticeService.pageList(new Page<>(pageNum, pageSize),noticeTitle,
                createBy, noticeType, beginTime, endTime);
        return Result.success(pageList);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据Id查询详情")
    public Result<SysNoticeEntity> get(@PathVariable Long id){
    	SysNoticeEntity sysNoticeEntity = isysNoticeService.getById(id);
    	return Result.success(sysNoticeEntity);
    }

    @PostMapping
    @ApiOperation("新增系统公告")
    @Log(title = "新增系统公告", businessType = BusinessType.INSERT)
    public Result<Boolean> save(@RequestBody SysNoticeEntity sysNoticeEntity){
        Boolean save = isysNoticeService.save(sysNoticeEntity);
        return  Result.success(save);
    }


    @PutMapping
    @ApiOperation("修改系统公告")
    @Log(title = "修改系统公告", businessType = BusinessType.UPDATE)
    public Result<Boolean> update(@RequestBody SysNoticeEntity sysNoticeEntity){
        boolean updateById = isysNoticeService.updateById(sysNoticeEntity);
        return  Result.success(updateById);
    }


    @DeleteMapping("/{ids}")
    @ApiOperation("删除系统公告")
    @Log(title = "删除系统公告", businessType = BusinessType.DELETE)
    public Result<Integer> delete(@PathVariable Integer[] ids){
    	List<Integer> asList = Arrays.asList(ids);
    	int deleteBatchIds = sysNoticeMapper.deleteBatchIds(asList);
    	return Result.success(deleteBatchIds);
    }

}
